
package com.eyedog.video.splash;

import android.util.Log;

import com.eyedog.library.utils.SharedPreferencesUtil;
import com.eyedog.video.VApplication;
import com.eyedog.video.adv.AdvInstance;
import com.eyedog.video.entity.ADVEntity;
import com.eyedog.video.entity.SplashEntity;
import com.eyedog.video.http.ICallback;

import java.util.Arrays;

public class ADVModel implements constract.IADVModel {

    private final String TAG = "ADVModel";

    @Override
    public void loadFirstSplash(final ICallback<SplashEntity> callback) {
        if (AdvInstance.getInstance().hasAdvInited()) {
            Log.i(TAG, "hasAdvInited true");
            ADVEntity advEntity = AdvInstance.getInstance().getSplashAdv();
            SplashEntity splashEntity = new SplashEntity(5000, Arrays.asList(advEntity));
            callback.onLoadSuccess(splashEntity);
        } else {
            Log.i(TAG, "hasAdvInited false");
            AdvInstance.getInstance().initAdv(new ICallback<Boolean>() {
                @Override
                public void onLoadSuccess(Boolean aBoolean) {
                    Log.i(TAG, "onLoadSuccess " + aBoolean);
                    ADVEntity advEntity = AdvInstance.getInstance().getSplashAdv();
                    SplashEntity splashEntity = new SplashEntity(5000, Arrays.asList(advEntity));
                    callback.onLoadSuccess(splashEntity);
                }

                @Override
                public void onLoadFailed(String err) {
                    callback.onLoadFailed(err);
                    Log.i(TAG, "onLoadFailed " + err);
                }
            });
        }
    }

    @Override
    public void loadSplash(final ICallback<SplashEntity> callback) {
        if (AdvInstance.getInstance().hasAdvInited()) {
            ADVEntity advEntity = AdvInstance.getInstance().getSplashAdv();
            SplashEntity splashEntity = new SplashEntity(5000, Arrays.asList(advEntity));
            callback.onLoadSuccess(splashEntity);
        } else {
            AdvInstance.getInstance().initAdv(new ICallback<Boolean>() {
                @Override
                public void onLoadSuccess(Boolean aBoolean) {
                    ADVEntity advEntity = AdvInstance.getInstance().getSplashAdv();
                    SplashEntity splashEntity = new SplashEntity(5000, Arrays.asList(advEntity));
                    callback.onLoadSuccess(splashEntity);
                }

                @Override
                public void onLoadFailed(String err) {
                }
            });
        }
    }

    @Override
    public void isFirstGuide(constract.GuideCallback callback) {
        boolean isFirstGuide = (boolean) SharedPreferencesUtil.get(VApplication.getAppContext(),
                "APPGUIDE", true);
        if (isFirstGuide) {
            SharedPreferencesUtil.put(VApplication.getAppContext(), "APPGUIDE", false);
        }
        callback.isFirstGuide(isFirstGuide);
    }
}
